package com.jic.manager.common.config.aop;

import com.jic.common.redis.RedisClientTemplate;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/*
* 数据隔离 条件默认添加 商家id 店铺id 供应商id 创建人id 修改人id
* wwb
* */
@SuppressWarnings("all")
@Aspect
@Component
@Slf4j
public class DataIsolationIntercept {

    @Resource
    private RedisClientTemplate redisClientTemplate;

    //申明一个切点 里面是 execution表达式
    @Pointcut("execution(public * com.jic.manager.controller.manage..*.*(..))")
    private void controllerAspect() {
    }

    //请求method前打印内容
    @Before(value = "controllerAspect()")
    public void methodBefore(JoinPoint joinPoint) throws RuntimeException {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        log.info("=========>>>>数据隔离开始<<<<======");
        Object[] args = joinPoint.getArgs();
        log.info("=========>>>>修改前参数: "+args);
//        if (args != null && args.length > 0) {
//            Object argument = args[0];
//            BeanWrapper beanWrapper = new BeanWrapperImpl(argument);
//            if (beanWrapper.isWritableProperty(ConstantClassField.TOKEN)) {
//                String token = (String) JSONObject.fromObject(argument).get(ConstantClassField.TOKEN);
//                String sysUserStr = redisClientTemplate.get(token);
//                if (StringUtils.isNotEmpty(sysUserStr)){
//                    SysUser sysUser = JSONArray.parseObject(sysUserStr, SysUser.class);
//                    //用户类型 0 运营用户 1 商家用户 2店铺用户 3供应商用户
//                    Integer type = sysUser.getType();
//                    //登录人ID
//                    Long sysId = sysUser.getId();
//                    if (type == 0){
//                        //运营方ID
//
//                    }else if(type == 1){
//                        if (beanWrapper.isWritableProperty(ConstantClassField.MERCHANT_ID)) {
//                            beanWrapper.setPropertyValue(ConstantClassField.MERCHANT_ID, sysUser.getMerchantId());
//                        }
//                    }else if(type == 2){
//                        if (beanWrapper.isWritableProperty(ConstantClassField.STORE_ID)) {
//                            beanWrapper.setPropertyValue(ConstantClassField.STORE_ID, sysUser.getSupplierId());
//                        }
//                    }
//                    //创建人名称
//                    if (beanWrapper.isWritableProperty(ConstantClassField.CREATOR_NAME)) {
//                        beanWrapper.setPropertyValue(ConstantClassField.CREATOR_NAME, sysUser.getUsername());
//                    }
//                    //修改人名称
//                    if (beanWrapper.isWritableProperty(ConstantClassField.UPDATOR_NAME)) {
//                        beanWrapper.setPropertyValue(ConstantClassField.UPDATOR_NAME, sysUser.getUsername());
//                    }
//                    //创建人
//                    if (beanWrapper.isWritableProperty(ConstantClassField.CREATOR)) {
//                        beanWrapper.setPropertyValue(ConstantClassField.CREATOR, sysId);
//                    }
//                    //修改人
//                    if (beanWrapper.isWritableProperty(ConstantClassField.UPDATOR)) {
//                        beanWrapper.setPropertyValue(ConstantClassField.UPDATOR, sysId);
//                    }
//                    //是否删除 Y N
//                    if (beanWrapper.isWritableProperty(ConstantClassField.IS_DELETE)) {
//                        beanWrapper.setPropertyValue(ConstantClassField.IS_DELETE, "N");
//                    }
//                }
//            }
//        }
    }
}
